package com.bigfish.controller;


import com.bigfish.service.PmsCategoryService;
import com.bigfish.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author Wp 544065263@qq.com
 * @since 1.0.0 2025-09-06
 */
@RestController
@RequestMapping("/product/category")
public class ProductCategoryController {
    @Autowired
    private PmsCategoryService pmsCategoryService;

    @RequestMapping("/list/tree")
    public R listTree() {
        List<Map<String, Object>> maps = pmsCategoryService.selectCategory();
        HashMap<Long, Map<String, Object>> m = new HashMap<>();
        List<Map<String, Object>> res = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Long catId = (Long) map.get("cat_id");
            m.put(catId, map);
            map.put("childrens", new ArrayList<>());
            res.add(map);
        }
        for (Map<String, Object> map : maps) {
            Long parentCid = (Long) map.get("parent_cid");
            if (m.get(parentCid) != null) {
                Map<String, Object> parent = m.get(parentCid);
                ((ArrayList) parent.get("childrens")).add(map);
            }
        }
        return R.ok(Map.of("data", res));
    }


}
